#include "AudioVideoPlayBar.h"
#include "ui_MediaPlayBar.h"
#include <QTime>
#include "qapplication.h"
#include "ApplicationIcon.h"

#include "mediaPositionSlider.h"
#include <QIcon>

#define TICK_INTERVAL_MS  100

#define PLAY_BUTTON_ICON_PATH   IconPath("track_play.png")
#define PAUSE_BUTTON_ICON_PATH  IconPath("track_pause.png")


AudioVideoPlayBar::AudioVideoPlayBar(IF_MediaEngineInterface &engine, MediaPositionSlider &seekbar,
                           QWidget *parent) :
   QGroupBox(parent),
   ui(new Ui::MediaPlayBar),
   m_engine(engine)
{
   ui->setupUi(this);
   ui->seekBarLayout->addWidget( &seekbar);

   ui->lcdTime->display("0:00.0");
   ui->bPlayPause->setFocusPolicy(Qt::NoFocus);

   // load icons
   ui->bPlayPause->setText("");     // only icons will be used
   m_playIcon  = new QIcon( QString( PLAY_BUTTON_ICON_PATH ));
   m_pauseIcon = new QIcon( QString( PAUSE_BUTTON_ICON_PATH ));

   ui->bPlayPause->setIconSize( QSize( 11, 11) );
   ui->bPlayPause->setIcon( *m_playIcon);

   engine.setTickInterval( TICK_INTERVAL_MS);

   // connections
   connect( &engine, SIGNAL(stateChanged(AudioObject::State)),
            this, SLOT(onSateChanged(AudioObject::State)) );
   connect( &engine, SIGNAL(totalTimeChanged(qint64)),
            this, SLOT(onTotalTimeChanged(qint64)) );
   connect( &engine, SIGNAL(tick(qint64)),
            this, SLOT(updateTime(qint64)) );
}

AudioVideoPlayBar::~AudioVideoPlayBar()
{
   delete ui;
}

/************************************************/
/* updateTime                                  */
/*  update lcd display from a number of ms      */
/************************************************/
void  AudioVideoPlayBar::updateTime(qint64 timeMs )
{
   QTime displayTime( 0, (timeMs / 60000) % 60, (timeMs / 1000) % 60, (timeMs % 1000) / 100 );

   ui->lcdTime->display( displayTime.toString("mm:ss.z") );
}

/**
 * To be called when duration of current media changes.
 * This include a track change.
 * @param timeMs - time in milliseocnds. When <= 0, no track
 *    is currently activated.
 */
void AudioVideoPlayBar::onTotalTimeChanged(qint64 timeMs)
{
   if (timeMs > 0)
   {
      updateTotalTime( timeMs);
   }
}

/************************************************/
/* updateTime                                  */
/*  update total time label from total ms       */
/************************************************/
void  AudioVideoPlayBar::updateTotalTime(qint64 timeMs )
{
   // update total time label
   QTime displayTime( 0, (timeMs / 60000) % 60, (timeMs / 1000) % 60 );

   ui->lbToalTime->setText( displayTime.toString("mm:ss") );
}

void AudioVideoPlayBar::onSateChanged(AudioObject::State newState)
{
   if (newState == AudioObject::PlayingState)
   {
      setPlaying();
   }
   else
   {
      setPaused();
   }
}

/************************************************/
/* setPlaying                                  */
/*  update graphic elements according to        */
/*  'playing' state                             */
/************************************************/
void  AudioVideoPlayBar::setPlaying()
{
   // show pause icon
   ui->bPlayPause->setIcon( *m_pauseIcon );
}

/************************************************/
/* setPaused                                   */
/*  update graphic elements according to        */
/*  'paused' state                              */
/************************************************/
void  AudioVideoPlayBar::setPaused()
{
   // show play icon
   ui->bPlayPause->setIcon( *m_playIcon );
}


void AudioVideoPlayBar::on_bPlayPause_clicked()
{
   m_engine.togglePlayPause();
}
